package com.biggerboy.controller;

import com.biggerboy.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@Slf4j
public class OrderController {

//    @Autowired
//    RestTemplate restTemplate;
//    @Autowired
//    DiscoveryClient discoveryClient;
    @Autowired
    UserService userService;

//    @RequestMapping("/create/{productId}")
//    public String create(@PathVariable Long productId) {
//        List<ServiceInstance> instances = discoveryClient.getInstances("service-user");
//        ServiceInstance serviceInstance = instances.get(0);
//        String host = serviceInstance.getHost();
//        int port = serviceInstance.getPort();
//        ResponseEntity<String> forEntity = restTemplate.getForEntity("http://" + host + ":" + port + "/user/detail/" + productId, String.class);
//        String body = forEntity.getBody();
//        log.info("body:{}", body);
//        return "ok";
//    }

    @RequestMapping("/create/v2/{productId}")
    public String createv2(@PathVariable Long productId) {
        String userDetail = userService.getUserDetail(productId);
        log.info("body:{}", userDetail);
        return "ok";
    }
}
